package com.sh.data.engine.repository.dao.datadev.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sh.data.engine.repository.dao.base.EngineBaseModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * offline dev node
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tb_datadev_offline_api", autoResultMap = true)
public class DatadevOfflineApiEntity extends EngineBaseModel<DatadevOfflineApiEntity> {
    private Long nodeId;

    /**
     * 任务名称
     */
    private String taskName;

    /**
     * 任务描述
     */
    private String remark;

    /**
     * 任务名称
     */
    private String nodeName;

    private String apiNodeType;

    private Long projectId;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private OfflineDevApiHttpEntity offlineDevApiHttpEntity;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private OfflineDevApiWsEntity offlineDevApiWsEntity;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private OfflineDevApiPreviewEntity offlineDevApiPreviewEntity;

    // 任务配置相关
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private TaskConfigRequest taskConfigRequest;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class TaskConfigRequest {
        // 目标数据源
        @NotNull
        private Long targetDsId;

        // 目标数据源类型
        @NotNull
        private String targetDsType;
        // 目标表
        @NotNull
        private String targetTableName;

        // 替换策略
        private Integer dataReplaceRuleId;
        // 同步前清空历史记录
        private Integer isTruncate;

        // 失败记录阈值
        private Integer errorNumLimit;

        private Integer fieldMapType;

        // 字段映射
        private List<FieldMapping> fieldMappings;

        private List<TargetField> targetFields;

        private List<PartitionField> partitions;

        /*生效开始时间*/
        @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
        private Date effectiveDateStart;

        /*生效结束时间*/
        @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
        private Date effectiveDateEnd;

        /*调度周期 0-月 1-周 2-天 3-小时 4-分钟*/
        private Integer cycle;

        /*执行开始时间的小时*/
        private String startTimeHour;

        /*执行开始时间的分钟*/
        private String startTimeMin;

        /*执行结束时间的小时*/
        private String endTimeHour;

        /*执行结束时间的分钟*/
        private String endTimeMin;

        /*
         * 月、周为执行日期 ，小时、分钟为执行间隔
         */
        private String execPoint;

        /*跨周期依赖 0-不依赖上一周期 1-等待上一周期结束*/
        private Integer cycleDependence;

        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public static class FieldMapping {
            private String sourceField;

            private String sourceFieldType;

            private Integer isSourceFieldPk;

            private String targetField;

            private String targetFieldType;

            private Integer isTargetFieldPk;

            private String targetComment;
        }

        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public static class TargetField {
            private String fieldName;

            private String fieldType;
        }

        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public static class PartitionField {

            @NotNull
            private String fieldName;

            @NotNull
            private String fieldValue;
        }
    }
}
